import express from 'express';
import { Nuxt, Builder } from 'nuxt';
import proxyMiddleware from 'http-proxy-middleware';

const router = express.Router();
const app = express();
// const HOST = '172.22.22.106';
const host = process.env.HOST || 'localhost';
const port = process.env.PORT || 7790;
let HOST = '221.214.90.154';

app.set('port', port);

// Import and Set Nuxt.js options
let config = require('../nuxt.config.js');
config.dev = !(process.env.NODE_ENV === 'production');

app.use(proxyMiddleware('/Aspx', {
  target: `http://${HOST}:8111`,
  secure: false,
  changeOrigin: false,
}));
// Init Nuxt.js
const nuxt = new Nuxt(config);

// Build only in dev mode
if (config.dev) {
  const builder = new Builder(nuxt);
  builder.build();
}
/*
router.post('/api', function (req, res, next) {
  res.json({
    contentList: [
      {
        time: '2017/11/14 12:14:30',
        content: '[浙江桐乡市公司濮院凯旋路分部]快件已被 已签收 签收',
      },
      {
        time: '2017/11/14 07:59:59',
        content: '[浙江桐乡市公司濮院凯旋路分部]进行派件扫描；派送业务员：牛玉杰；联系电话：15669333111',
      },
      {
        time: '2017/11/13 17:59:59',
        content: '[浙江嘉兴分拨中心]从站点发出，本次转运目的地：浙江桐乡市公司',
      },
      {
        time: '2017/11/13 7:29:59',
        content: '[浙江嘉兴分拨中心]在分拨中心进行卸车扫描',
      },
      {
        time: '2017/11/12 17:59:59',
        content: '[浙江杭州分拨中心]进行装车扫描，即将发往：浙江嘉兴分拨中心',
      },
      {
        time: '2017/11/12 07:22:59',
        content: '[黑龙江哈尔滨分拨中心]进行装车扫描，即将发往：浙江杭州分拨中',
      },
      {
        time: '2017/11/11 19:22:59',
        content: '[黑龙江哈尔滨南岗区哈西公司西兴服务站分部]进行揽件扫描',
      },
    ],
    totals: 7,
  });
});
*/

app.use(router);

// Give nuxt middleware to express
app.use(nuxt.render);

// Listen the server
app.listen(port, host);
console.log('Server listening on ' + host + ':' + port); // eslint-disable-line no-console
